package online.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import online.config.GlobleConfig;
import online.entity.UserDao;
import online.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 狼芒
 * @Date: 2021/8/23 22:53
 * @Descrintion: 用户登录控制器
 * @version: 1.0
 */
@Controller//控制层
@Slf4j//方便的打印日志
public class LoginController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/version")
    @ResponseBody
    public String version() {
        return "1.0";
    }

    @RequestMapping({"/", "/login"})
    @ResponseBody
    public String login(UserDao userDao) {
        System.out.println(userDao);
        GlobleConfig.globleSession.setAttribute("userDao", userDao);
        return userDao.toString();
    }

    @GetMapping("/getUser")
    @ResponseBody
    public Object getUser() {
        String sql = "select * from system_user";
        List<Map<String, Object>> allList = jdbcTemplate.queryForList(sql);
        log.error("查询用户为：{}", allList);
        return allList;
    }

    @Autowired
    LoginService loginService;

    @GetMapping("/getUser/{id}")
    @ResponseBody
    public Object getUser(@PathVariable("id") Long id) {
        UserDao user = loginService.getUser(id);
        return user;
    }



    @GetMapping("/getAllUser")
    @ResponseBody
    public List<UserDao> getAllUser(){
        return loginService.getAllUser();
    }


    @GetMapping("/getMybatisPlusUser")
    @ResponseBody
    public UserDao getMybatisPlusUser(){
        return loginService.getMybatisPlusUser();
    }


    @GetMapping("/getMybatisPlusAllUser")
    @ResponseBody
    public List<UserDao> getMybatisPlusAllUser(){
        return loginService.list();
    }


    @GetMapping("/getMybatisPlusPageUser/{current}/{size}")
    @ResponseBody
    public IPage<UserDao> getMybatisPlusPageUser(@PathVariable("current") Integer current, @PathVariable("size") Integer size){
        IPage<UserDao> page = new Page<>(current,size);
        return loginService.page(page, null);
    }






    @RequestMapping("/getUsers")
    @ResponseBody
    public String getUsers() {
        return new UserDao().toString();
    }

    @RequestMapping("/error1")
    @ResponseBody
    public String error() {
        return new UserDao().toString();
    }

}
